package net.orange.framework.logic;

import java.util.Collection;

import net.orange.architecture.db.Condition;
import net.orange.architecture.db.Conditions;
import net.orange.architecture.process.BaseProcess;
import net.orange.framework.model.Department;
import net.orange.framework.model.Group;
import net.orange.framework.model.User;

/**
 * The user business logical process object which implement the IUserProcess
 * interface.
 */
public class UserProcess extends BaseProcess implements IUserProcess {
	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.framework.user.process.IUserProcess#validate(java.lang.String,
	 *      java.lang.String)
	 */
	public boolean validate(String login_no, String login_pwd) throws Exception {
		// return false if login no is empty.
		if (login_no == null || login_no.trim().length() <= 0)
			return false;

		User user = (User) dao.find("login_no", login_no);

		return (user != null && user.getLogin_pwd() != null && user
				.getLogin_pwd().equals(login_pwd));
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.framework.logic.IUserProcess#find(net.orange.framework.model.Department)
	 */
	public Collection find(Department department) throws Exception {
		if (department == null)
			return null;

		Conditions cnds = new Conditions();
		cnds.add(new Condition("department.pk", department.getPk()));

		return dao.list(cnds);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.framework.logic.IUserProcess#find(net.orange.framework.model.Group)
	 */
	public Collection find(Group group) throws Exception {
		if (group == null)
			return null;

		Conditions cnds = new Conditions();
		cnds.add(new Condition("groups.pk", group.getPk()));

		return dao.list(cnds);
	}
}